const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const FORM = 'FORM';
const DELETE = 'DELETE';

const baseURL = getApp().url;

function request(method, url, data) {
  return new Promise(function (resolve, reject) {
    let header = {
      "Content-Type": "application/json",
      "token": wx.getStorageSync('token') || '',
    };
    wx.request({
      url: baseURL + url,
      method: method,
      data: method === POST ? JSON.stringify({
        ...data,

      }) : method === PUT ? {
        ...data,

      } : {},
      header: header,
      success(res) {
        if (res.statusCode == 200) {
          if (null == res.status || res.status == "") {
            //请求成功
            //判断状态码---errCode状态根据后端定义来判断
            if (res.data.status == 200) {
              resolve(res.data);
            } else {
              //如果需要处理不是200的错误 在这里处理
              resolve(res.data);
            }
          } else {
            wx.showToast({
              title: '未知错误',
              icon: 'error'
            })
          }
        }
        if (res.statusCode === 401) {
          wx.showModal({
            title: '身份异常',
            content: '请重新登录',
            showCancel: false,
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后
                wx.navigateTo({
                  url: '/pages/login/login',
                })
              } else { //这里是点击了取消以后
                // console.log('用户点击取消')
              }
            }
          })
        }
        //身份为错误或者参数为空
        else if (res.statusCode == 999) {
          console.log(res)
          //身份异常
          wx.showModal({
            title: '身份异常',
            content: '请重新登录',
            showCancel: false,
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后
                wx.navigateTo({
                  url: '/pages/denglu/denglu',
                })
              } else { //这里是点击了取消以后
                // console.log('用户点击取消')
              }
            }
          })
          //404 报错
        } else if (res.statusCode == 404) {
          wx.showToast({
            title: '访问服务器出现意外!',
            icon: 'error'
          })
        } else if (res.statusCode == 500) {
          wx.showToast({
            title: '500',
            icon: 'error'
          })
        }
      },
      fail(err) {
        console.log(err)

        //请求失败
        wx.showModal({
          title: '连接失败',
          content: '请稍候重试',
          success: function (res) {
            wx.navigateBack();
          }
        })
      }
    })
  })
}

const API = {
  // 登录
  loginLand: (data) => request(POST, 'ap/login/land', data),
  // 注册
  register: (data) => request(POST, 'ap/login/register', data),
  // 获取试题
  getDaily: (data) => request(POST, 'ap/prb/get/daily', data),
  // 答题提交
  prbSubmit: (data) => request(POST, 'ap/prb/submit', data),
  // 答题历史日期
  dateList: (data) => request(GET, 'ap/answer/date-list/' + data),
  // 获取答题历史详情
  detailList: (data) => request(GET, `ap/answer/detail-list/${data.userId}/${data.date}`),
  // 收藏试题
  collectSubmit: (data) => request(POST, 'ap/collect/submit', data),
  // 获取收藏列表
  collectList: (data) => request(GET, `ap/collect/list/${data}`),
  // 取消收藏
  collectCancel: (data) => request(POST, "ap/collect/cancel", data),
  // 获取错题记录表日期
  wrongList: (data) => request(GET, `ap/wrong/date/list/${data}`),
  // 获取收藏详情
  collectDetailList: (data) => request(GET, `ap/collect/detail-list/${data.userId}/${data.date}`),
  // 获取错题回顾详情
  wrongDetailList: (data) => request(GET, `ap/wrong/detail-list/${data.userId}/${data.date}`),



  // 查询签到时间  
  selectTime: (data) => request(POST, "sign/selectTime", data),
  // 签到
  signin: (data) => request(POST, "sign/in", data),
  // 点赞
  fabulous: (data) => request(POST, "posts/fabulous", data),
  // .收藏 POST
  collection: (data) => request(POST, "posts/collection", data),

  // 评论展示 POST
  showComment: (data) => request(POST, "posts/show-comment", data),
  // 评论 POST
  comment: (data) => request(POST, "posts/comment", data),
  // 评论回复 POST
  commentReply: (data) => request(POST, "posts/comment-reply", data),
  // 查询个人主页帖子
  userPosts: (data) => request(POST, "posts/select/user-posts", data),
  // 关注查询
  selectState: (data) => request(POST, "follow/select-state", data),

  // 我关注的
  myFollow: (data) => request(POST, "posts/select/my-follow", data),
  // 关注我的
  followMy: (data) => request(POST, "posts/select/follow-my", data),
  // 关注
  state: (data) => request(POST, "follow/state", data),
  // 我的评论
  myComment: (data) => request(POST, "posts/select/my-comment", data),
  // 我的收藏
  myCollections: (data) => request(POST, "posts/select/my-collections", data),
  // 我的点赞
  myFabulous: (data) => request(POST, "posts/select/my-fabulous", data),

  // 删除评论回复
  removeCommentReply: (data) => request(POST, "posts/remove-commentReply", data),
  // 我的帖子
  myPosts: (data) => request(POST, "posts/select/my-posts", data),
  // 删除评论
  removeComment: (data) => request(POST, "posts/remove-comment", data),
  // 二手发布
  insertShop: (data) => request(POST, "shop/insert-shop", data),
  // 商品封面查询
  selectShop: (data) => request(POST, "shop/select-shop", data),
  // 商品封面查询
  selectShopLike: (data) => request(POST, "shop/select-shop-like", data),
  // 查询积分
  selectUserIntegral: (data) => request(POST, "userinformation/select-user-integral", data),
  // 删除帖子
  removePosts: (data) => request(POST, "posts/remove-posts", data),
  // 查询二手商品详情
  selectDetailedShop: (data) => request(GET, "shop/select-detailed-shop/" + data, ''),
  // 查询帖子评论数
  commentNum: (data) => request(GET, "posts/select/comment-num/" + data, ''),
  // 举报功能
  reportPosts: (data) => request(POST, "posts/report-posts", data),
};
module.exports = {
  API: API
}